﻿package com.thorqq.magictimer.util;

import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{
    public static final String[] ZH_WEEK = new String[]{"日","一","二","三","四","五","六"};
    public static final String[] ZH_LUNAR_MON = new String[]{"正","二","三","四","五","六","七","八","九","十","十一","腊"};
    public static final String[] ZH_MON = new String[]{"一","二","三","四","五","六","七","八","九","十","十一","十二"};
    public static final String[] ZH_WEEK_IN_MON = new String[]{"一","二","三","四","最后一"};
    public static final String[] ZH_DAY_IN_MON = new String[]{
        "一","二","三","四","五","六","七","八","九","十",
        "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
        "二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一","最后一天"};
    public static final String[] ZH_LUNAR_DAY_IN_MON = new String[]{
        "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
        "十一","十二","十三","十四","十五","十六","十七","十八","十九","廿",
        "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十","三十一","最后一天"};

    public static String getDate()
    {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
                .format(new Date());
    }

    // 将字符串 yyyy-MM-dd HH:mm:ss转换为时间类型
    public static Date strToDate(String strDate, String format)
    {
        SimpleDateFormat formatter = null;
        Date strtodate = null;

        try
        {
            if (format == null)
            {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                formatter = new SimpleDateFormat(format);
            }

            strtodate = formatter.parse(strDate, new ParsePosition(0));
        }
        catch (Exception e)
        {
            Util.log_ex(e);
        }

        return strtodate;
    }

    public static Date strToDate(String strDate)
    {
        return strToDate(strDate, null);
    }

    public static Date yyyymmddToDate(String strDate)
    {
        return strToDate(strDate, "yyyy-MM-dd");
    }

    public static Calendar getCalendar(String strDate)
    {
        Date dd = yyyymmddToDate(strDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dd);

        return calendar;
    }

    // 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
    public static String dateToStr(Date dateDate, String format)
    {
        SimpleDateFormat formatter = null;
        String dateString = null;
        try
        {
            if (format == null)
            {
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                formatter = new SimpleDateFormat(format);
            }

            dateString = formatter.format(dateDate);
        }
        catch (Exception e)
        {
            Util.log_ex(e);
        }
        return dateString;
    }

    public static String dateToStr(Date dateDate)
    {
        return dateToStr(dateDate, null);
    }

    public static String dateToYYYYMMDD(Date dateDate)
    {
        return dateToStr(dateDate, "yyyy-MM-dd");
    }

    static private Calendar tmpForMillisToStr1 = Calendar.getInstance();
    public static String MillisToStr(long millis)
    {
        tmpForMillisToStr1.setTimeInMillis(millis);
        return dateToStr(tmpForMillisToStr1.getTime());
    }

    public static String formatTwoNumber(int n)
    {
        return new DecimalFormat("00").format(n);
    }

    static private Calendar tmpForMillisToStr2 = Calendar.getInstance();
    public static String MillisToHHMM(long millis)
    {
        tmpForMillisToStr2.setTimeInMillis(millis);
        return ""
                + formatTwoNumber(tmpForMillisToStr2.get(Calendar.HOUR_OF_DAY))
                + ":"
                + formatTwoNumber(tmpForMillisToStr2.get(Calendar.MINUTE));
    }

    public static String MillisToHHMM(Calendar c)
    {
        return ""
                + formatTwoNumber(c.get(Calendar.HOUR_OF_DAY))
                + ":"
                + formatTwoNumber(c.get(Calendar.MINUTE));
    }

    static private Calendar tmpForMillisToStr3 = Calendar.getInstance();
    public static String MillisToYYYYMMDD(long millis)
    {
        tmpForMillisToStr3.setTimeInMillis(millis);
        return "" + formatTwoNumber(tmpForMillisToStr3.get(Calendar.YEAR))
                + "-" + formatTwoNumber(tmpForMillisToStr3.get(Calendar.MONTH)+1)
                + "-" + formatTwoNumber(tmpForMillisToStr3.get(Calendar.DATE));
    }

    public static String MillisToYYYYMMDD(Calendar c)
    {
        return "" + formatTwoNumber(c.get(Calendar.YEAR))
                + "-" + formatTwoNumber(c.get(Calendar.MONTH)+1)
                + "-" + formatTwoNumber(c.get(Calendar.DATE));
    }

    public static String MillisToWeek(Calendar c)
    {
        return "星期" + ZH_WEEK[c.get(Calendar.DAY_OF_WEEK)-1];
    }

//    static private ChineseCalendar tmpCaloStr4 = new ChineseCalendar();
//    public static String MillisToChineseCalendar(Calendar c)
//    {
//        tmpCaloStr4.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
//        return tmpCaloStr4.getCompleteChineseDateString();
//    }
    
    public static String formatTime(Calendar c)
    {
        return MillisToHHMM(c.getTimeInMillis());
    }
    
    //
    public static String intervalToString(long a, long b)
    {
        long interval = Math.abs(a - b)/1000;
        if(interval >= 0 && interval <= 60)
        {
            return "一分钟内";
        }
        else if(interval > 60 && interval <= 60 * 60)
        {
            return interval/60 + "分钟后";
        }
        else if(interval > 60 * 60 && interval <= 60 * 60 * 24)
        {
            return interval/(60*60) + "小时后";
        }
        else if(interval > 60 * 60 * 24 && interval <= 60 * 60 * 24 * 31)
        {
            return interval/(60*60*24) + "天后";
        }
        else if(interval > 60 * 60 * 24 * 31 && interval <= 60 * 60 * 24 * 365)
        {
            return interval/(60*60*24*31) + "个月后";
        }
        else if(interval > 60 * 60 * 24 * 365)
        {
            return interval/(60*60*24*365) + "年后";
        }
        return null;
    }

    //计算两天相隔的天数
    public static int intervalDays(Calendar c1, Calendar c2)
    {
        //不能修改年月日，因为c1,c2可能是阴历
        Calendar a1 = (Calendar) c1.clone();
        a1.set(Calendar.HOUR_OF_DAY, 0);
        a1.set(Calendar.MINUTE, 0);
        a1.set(Calendar.SECOND, 0);
        
        Calendar a2 = (Calendar) c2.clone();
        a2.set(Calendar.HOUR_OF_DAY, 0);
        a2.set(Calendar.MINUTE, 0);
        a2.set(Calendar.SECOND, 0);
        
        //除以1000，舍掉后面的微秒
        long tmp = a1.getTimeInMillis()/1000 - a2.getTimeInMillis()/1000;
        final int oneDay = 24 * 60 * 60;
        
        if(tmp%oneDay == 0)
        {
            return (int)(tmp/oneDay);
        }
        else
        {
            return (int)(tmp/oneDay) + 1;
        }

    }
}
